var NotificationCenter = require('../tools/SimpleNotificationCenter');

cc.Class({
    extends: cc.Component,

    properties: {

        // //是否吞噬touch事件
        // isSwallowTouch: false,

        //火星坐标
        longlongPosition: {
            default: cc.p(999, 999),
            type: cc.Vec2
        },

        //SeedStore
        seedStore: {
            default: null,
            type: cc.Node
        }
    },

    // use this for initialization
    onLoad: function () {

        //设置事件监听
        NotificationCenter.observe('showSeedStore', this.notifiyHandler, this);
        NotificationCenter.observe('hideSeedStore', this.notifiyHandler, this);
    },
    //干掉NotificationCenter的观察者
    onDestroy: function name(params) {
        NotificationCenter.removeObserve('showSeedStore', this.notifiyHandler);
        NotificationCenter.removeObserve('hideSeedStore', this.notifiyHandler);
    },


    //初始化UI
    initUI: function () {
        this.seedStore.active = false;
        this.seedStore.position = this.longlongPosition;
    },

    //由于暂时还未参悟透节点事件 所以只能这么可耻的实现了
    setSwallow: function () {
        this.node.on('touchend', this.swallowEvent);
    },
    unsetSwallow: function () {
        this.node.off('touchend', this.swallowEvent);
    },
    //吞噬事件
    swallowEvent: function (event) {
        cc.log("SwallowEvent");
        //停止冒泡
        event.stopPropagation();
    },
    //显示种子商店
    showSeedStore: function () {

        //设置吞噬为
        this.setSwallow();
        //重设坐标到屏幕内
        this.seedStore.position = cc.p(0, 0);
        //设置seedStore为激活状态
        this.seedStore.active = true;
    },
    //隐藏种子商店
    hideSeedStore: function () {
        this.unsetSwallow();
        this.seedStore.position = this.longlongPosition;
    },

    //处理全局通知
    notifiyHandler: function (event, sender, data) {
        switch (event) {
            case "showSeedStore":
                this.showSeedStore();
                break;
            case "hideSeedStore":
                this.hideSeedStore();
                break;

            default:
                cc.log("Not handled event", event);
                break;
        }
    }
});
